<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="服务项名称" prop="serverName">
      <el-input v-model="dataForm.serverName" placeholder="服务项名称"></el-input>
    </el-form-item>
    <el-form-item label="代理收益" prop="dlSy">
      <el-input v-model="dataForm.dlSy" placeholder="代理收益"></el-input>
    </el-form-item>
    <el-form-item label="用户收益" prop="userSy">
      <el-input v-model="dataForm.userSy" placeholder="用户收益"></el-input>
    </el-form-item>
    <el-form-item label="平台收益" prop="pSy">
      <el-input v-model="dataForm.pSy" placeholder="平台收益"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="creareTime">
      <el-input v-model="dataForm.creareTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="1 展示;2 未展示" prop="isShow">
      <el-input v-model="dataForm.isShow" placeholder="1 展示;2 未展示"></el-input>
    </el-form-item>
    <el-form-item label="价格规则{s:1,m:2,l:3}" prop="priceGui">
      <el-input v-model="dataForm.priceGui" placeholder="价格规则{s:1,m:2,l:3}"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="des">
      <el-input v-model="dataForm.des" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="图标" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="图标"></el-input>
    </el-form-item>
    <el-form-item label="接单人" prop="jdr">
      <el-input v-model="dataForm.jdr" placeholder="接单人"></el-input>
    </el-form-item>
    <el-form-item label="收益ID" prop="rateId">
      <el-input v-model="dataForm.rateId" placeholder="收益ID"></el-input>
    </el-form-item>
    <el-form-item label="标签" prop="tags">
      <el-input v-model="dataForm.tags" placeholder="标签"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          serverName: '',
          dlSy: '',
          userSy: '',
          pSy: '',
          creareTime: '',
          isShow: '',
          priceGui: '',
          des: '',
          icon: '',
          jdr: '',
          rateId: '',
          tags: ''
        },
        dataRule: {
          serverName: [
            { required: true, message: '服务项名称不能为空', trigger: 'blur' }
          ],
          dlSy: [
            { required: true, message: '代理收益不能为空', trigger: 'blur' }
          ],
          userSy: [
            { required: true, message: '用户收益不能为空', trigger: 'blur' }
          ],
          pSy: [
            { required: true, message: '平台收益不能为空', trigger: 'blur' }
          ],
          creareTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          isShow: [
            { required: true, message: '1 展示;2 未展示不能为空', trigger: 'blur' }
          ],
          priceGui: [
            { required: true, message: '价格规则{s:1,m:2,l:3}不能为空', trigger: 'blur' }
          ],
          des: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '图标不能为空', trigger: 'blur' }
          ],
          jdr: [
            { required: true, message: '接单人不能为空', trigger: 'blur' }
          ],
          rateId: [
            { required: true, message: '收益ID不能为空', trigger: 'blur' }
          ],
          tags: [
            { required: true, message: '标签不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/bbl/dlserver/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.serverName = data.dlServer.serverName
                this.dataForm.dlSy = data.dlServer.dlSy
                this.dataForm.userSy = data.dlServer.userSy
                this.dataForm.pSy = data.dlServer.pSy
                this.dataForm.creareTime = data.dlServer.creareTime
                this.dataForm.isShow = data.dlServer.isShow
                this.dataForm.priceGui = data.dlServer.priceGui
                this.dataForm.des = data.dlServer.des
                this.dataForm.icon = data.dlServer.icon
                this.dataForm.jdr = data.dlServer.jdr
                this.dataForm.rateId = data.dlServer.rateId
                this.dataForm.tags = data.dlServer.tags
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/bbl/dlserver/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'serverName': this.dataForm.serverName,
                'dlSy': this.dataForm.dlSy,
                'userSy': this.dataForm.userSy,
                'pSy': this.dataForm.pSy,
                'creareTime': this.dataForm.creareTime,
                'isShow': this.dataForm.isShow,
                'priceGui': this.dataForm.priceGui,
                'des': this.dataForm.des,
                'icon': this.dataForm.icon,
                'jdr': this.dataForm.jdr,
                'rateId': this.dataForm.rateId,
                'tags': this.dataForm.tags
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
